package org.demo.ftp;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.apache.commons.net.ProtocolCommandEvent;
import org.apache.commons.net.ProtocolCommandListener;

/**
 * 
 * @author  
 * @date    2011-1-23
 * @file    org.demo.ftp.PrintConsoleListener.java
 */
public class PrintConsoleListener implements ProtocolCommandListener {

	private final PrintWriter __writer;
	
	public PrintConsoleListener(PrintWriter writer)
    {
        __writer = writer;
    }
	
	@Override
	public void protocolCommandSent(ProtocolCommandEvent event) {
		String msg = event.getMessage();
		try {
			msg = new String(msg.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// Ignore.
		}
		__writer.print(msg);
        __writer.flush();
	}

	@Override
	public void protocolReplyReceived(ProtocolCommandEvent event) {
		String msg = event.getMessage();
		try {
			msg = new String(msg.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// Ignore.
		}
		__writer.print(msg);
        __writer.flush();
	}
}
